package com.test.daily.leetcode.y2022.m04.day0430.v1;

/**
 * Solution
 *
 * @author Tom on 2022/4/30
 */
public class Solution {
    public static void main(String[] args) {
        int[] nums = new int[]{9, 9, 2, 8, 7};
        int k = 4;
        System.out.println(new Solution().smallestRangeI(nums, k));
    }

    public int smallestRangeI(int[] nums, int k) {

        int N = nums.length;
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < N; i++) {
            min = Math.min(min, nums[i]);
            max = Math.max(max, nums[i]);
        }
        return max - min >= 2 * k ? (max - min - 2 * k) : 0;
    }
}
